package com.iamback.dy.examine.controller.admin.examine;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iamback.dy.entity.AgainContentExamineView;
import com.iamback.dy.entity.ContentExuser;
import com.iamback.dy.entity.User;
import com.iamback.dy.examine.common.JsonResult;
import com.iamback.dy.examine.common.Page;
import com.iamback.dy.examine.controller.BaseController;
import com.iamback.dy.examine.controller.modle.AgainContentExamineVo;
import com.iamback.dy.examine.service.IContentExuserService;
import com.iamback.dy.examine.service.IContentUnreviewedService;
import com.iamback.dy.examine.service.IDyContentService;

/**
 * 帖子的回审
 */
@Controller
@RequestMapping("/examine/again/content")
public class AgainContentController extends BaseController {

	@Autowired
	private IContentExuserService contentExuserService;
	@Autowired
	private IContentUnreviewedService contentUnreviewedService;
	@Autowired
	private IDyContentService contentService;
	@RequestMapping("/show")
	public String showContentExamine(){
		return "examine/againContent";
		
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public Page<AgainContentExamineView> getAgainContentExamineView(Integer pageNumber,Integer  pageSize){
    	User user =(User) SecurityUtils.getSubject().getSession().getAttribute("loginUserInfo");
    	Date starDate = getStartTime();
    	Page<AgainContentExamineView> page =contentExuserService.findAgainContentExamineByPage(user.getId(),starDate,pageNumber,pageSize);
		return page;
    }
	
	@RequestMapping("/update")
	@ResponseBody
    public JsonResult updateContentExamine(@RequestBody AgainContentExamineVo againContentExamineVo){
		User user =(User) SecurityUtils.getSubject().getSession().getAttribute("loginUserInfo");
		ContentExuser exuserContent= contentExuserService.findContentExuserById(againContentExamineVo.getId());
		String exResult =againContentExamineVo.getExResult();
		if(exuserContent!=null){
			if(!againContentExamineVo.getExResult().equals(exuserContent.getExResult())){
				ContentExuser contentExuser =new ContentExuser();
				contentExuser.setId(exuserContent.getId());
				exuserContent.setExResult(againContentExamineVo.getExResult());
				exuserContent.setExuserId(Long.valueOf(user.getId()));
				exuserContent.setExTime(new Date());
				contentExuserService.updateContentExuser(exuserContent);
				Map<String, Object> map =new HashMap<String, Object>();
				map.put("isApproved", againContentExamineVo.getExResult());
				map.put("dyContentId", exuserContent.getDyContentId());
				contentUnreviewedService.updateUnreviewed(map);
				if(exResult.equals("2")){ //通过修改为不通过 
					//将通过删除为 dy_content 表中的帖子
					contentUnreviewedService.deleteDyContent(exuserContent.getDyContentId());
				}else{
					contentService.insertContentByex(exuserContent.getDyContentId()); 
				} 
				
			}
		}
		return JsonResult.success();
    }
    
	
    public static Date getStartTime() {
		Calendar todayStart = Calendar.getInstance();
		todayStart.set(Calendar.HOUR_OF_DAY, 0);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		return todayStart.getTime();
	}
}
